<?php
namespace app\api\controller;
use think\Request;
use app\api\controller\Base;
use app\api\model\UserAddress as MuserAdress;

/**
 * @title adress 收货地址接口|[客户端]
 * @description 用户管理收货地址接口
 */
 
class UserAddress extends Base
{
    /**
     * @title address/list 查询收货地址相关信息 GET
     * @description 说明：依据 user_id 查询收货地址相关信息 
     * @author 虾哥@2424899422@qq.com
     * @url address/list/
	 * @Turl api/user_address/lists
     *
	 * @param name:user_id type:int require:1 default:null other: desc:用户ID
	 * @param name:page type:int require:0 default:1 other: desc:当前页ID
	 * @param name:length type:int require:0 default:4 other: desc:当前页显示数量
	 *
	 * @return lists:地址列表|参考备注信息@
     * @lists address_id:收货地址自增ID  user_name:收货人姓名 user_tel:收货人手机号码 user_phone:收货人电话号码 province_id:收货人省份id city_id:收货人市id country_id:收货人区县id address:收货人详细地址  post_code:收货人邮编 is_default:是否设为默认地址(0否 | 1是)
	 *
	 * @remark 
	 *
	 * @example 返回示例{"code":0,"message":"query address list success !","data":{"list":[{"address_id":4,"user_name":"是范德萨","user_tel":"4124656","user_phone":"","province_id":0,"city_id":0,"country_id":0,"address":"","post_code":"","is_default":0},{"address_id":5,"user_name":"1","user_tel":"","user_phone":"","province_id":0,"city_id":0,"country_id":0,"address":"","post_code":"","is_default":0},{"address_id":7,"user_name":"水电费","user_tel":"1234564687546","user_phone":"123456456784","province_id":0,"city_id":0,"country_id":0,"address":"","post_code":"","is_default":0}]}}
     */
    public function lists($user_id,$page,$length)
    {
		
		$return = array('code' => 0, 'message' => 'query address list success !', 'data' => array());
		$data = MuserAdress::lists($user_id,$page,$length);
		if (empty($data)) {
            $return['code'] = 1;
            $return['message'] = 'query address list failed !';
            return $return;
        }
        $return['data']['list'] = $data;
		return $return;	
    }
	
	
	/**
     * @title address/create 添加收货地址相关信息 POST
     * @description 说明：依据 user_id 添加收货地址相关信息 |【暂不支持批量添加】
	 * @author 虾哥@2424899422@qq.com
	 * @url address/create/
	 * @Turl api/user_address/create
	 
	 * @param name:user_id type:int require:1 default:null other: desc: 用户ID
	 * @param name:user_name type:string require:1 default:'' other: desc:收货人姓名
	 * @param name:user_tel type:string require:1 default:'' other: desc:收货人手机号码
	 * @param name:user_phone type:string require:1 default:'' other: desc:收货人电话号码
	 * @param name:province_id type:int require:1 default:0 other: desc:收货人省份id
	 * @param name:city_id type:int require:1 default:0 other: desc:收货人市id
	 * @param name:country_id type:int require:1 default:0 other: desc:收货人区县id
	 * @param name:address type:string require:1 default:1 other: desc:收货人详细地址
	 * @param name:post_code type:string require:1 default:'' other: desc:收货人邮编
	 * @param name:is_default type:int require:1 default:1 other: desc:是否设为默认地址 1 默认设置
	 
	 * @return lists:返回影响行数@
     * @lists nums:0失败|大于0为新增行数
     * 
     */
    public function create() {
	   $data = Request::instance()->post();
	   $return = array('code' => 0, 'message' => 'create address success !', 'data' => array());
	   $data['create_time'] = time();
	   $data = MuserAdress::addressAdd($data);
	   if (empty($data)) {
			$return['code'] = 1;
			$return['message'] = 'create address failed !';
			$return['data']['nums'] = 0;
			return $return;
		}
		$return['data']['nums'] = $data;
		return json($return);	
	}
	
	/**
     * @title address/delete 删除收货地址相关信息 GET
     * @description 依据 address_ids 进行删除收货地址|【暂不支持批量删除】
	 * @author 虾哥@2424899422@qq.com
	 * @url address/delete/
	 * @Turl api/user_address/delete
	 
	 * @param name:address_id type:int require:1 default:null other: desc:收货地址自增ID集合
	 
	 * @return lists:返回影响行数@
     * @lists nums:0失败|大于0为删除行数
     *
     */
    public function delete($address_id) {
        $return = array('code' => 0, 'message' => 'delete address success !', 'data' => array());
		$data = MuserAdress::addressDelete($address_id);
		if (empty($data)) {
			$return['code'] = 1;
			$return['message'] = 'delete address failed !';
			$return['data']['nums'] = 0;
			return $return;
		}
		$return['data']['nums'] = $data;
		return json($return);	
    }
	
	
	/**
     * @title address/update 更新收货地址相关信息 POST
     * @description 依据 address_id 更新收货地址相关信息 
	 * @author 虾哥@2424899422@qq.com
	 * @url address/update/
	 * @Turl api/user_address/update
  
	 * @param name:address_id type:int require:1 default:null other: desc:收货地址自增ID
	 * @param name:user_id type:int require:1 default:null other: desc: 用户ID
	 * @param name:user_name type:string require:1 default:'' other: desc:收货人姓名
	 * @param name:user_tel type:string require:1 default:'' other: desc:收货人手机号码
	 * @param name:user_phone type:string require:1 default:'' other: desc:收货人电话号码
	 * @param name:province_id type:int require:1 default:0 other: desc:收货人省份id
	 * @param name:city_id type:int require:1 default:0 other: desc:收货人市id
	 * @param name:country_id type:int require:1 default:0 other: desc:收货人区县id
	 * @param name:address type:string require:1 default:1 other: desc:收货人详细地址
	 * @param name:post_code type:string require:1 default:'' other: desc:收货人邮编
	 * @param name:is_default type:int require:1 default:1 other: desc:是否设为默认地址 1 默认设置
	 
	 * @return lists:返回影响行数@
     * @lists nums:0失败|大于0为更新行数
     */

    public function update() {
	   $data = Request::instance()->post();
	   $return = array('code' => 0, 'message' => 'update address success !', 'data' => array());
	   $data['create_time'] = time();
	   $data = MuserAdress::addressUpdate($data);
	   if (empty($data)) {
			$return['code'] = 1;
			$return['message'] = 'update address failed !';
			$return['data']['nums'] = 0;
			return $return;
		}
		$return['data']['nums'] = $data;
		return json($return);	 
    }

	
    /**
     * @title address/detail 获取收货地址详情 GET
     * @description 依据 address_id 获取收货地址详情
	 * @author 虾哥@2424899422@qq.com
	 * @url address/detail/
	 * @Turl api/user_address/detail
	 
	 * @param name:address_id type:int require:1 default:null other: desc:收货地址自增ID
	 
	 * @return lists:地址详情@
     * @lists address_id:收货地址自增ID  user_name:收货人姓名 user_tel:收货人手机号码 user_phone:收货人电话号码 province_id:收货人省份id city_id:收货人市id country_id:收货人区县id address:收货人详细地址  post_code:收货人邮编 
	 
     */
    public function detail($address_id) {
       $return = array('code' => 0, 'message' => 'get adress detail success !', 'data' => array());
		$data = MuserAdress::addressInfo($address_id);
		if (empty($data)) {
            $return['code'] = 1;
            $return['message'] = 'get adress detail failed！';
            return $return;
        }
        $return['data']['list'] = $data;
		return json($return);	
    }
	
    /**
     * @title address/set_default 设置默认收货地址 GET
     * @description 依据 address_id 设置默认收货地址
	 * @author 虾哥@2424899422@qq.com
	 * @url address/set_default/
	 * @Turl api/user_address/set_default
	 
     * @param name:address_id type:int require:1 default:null other: desc:收货地址自增ID
	 
	 * @return lists:返回影响行数@
     * @lists nums:0失败|大于0为成功
	 *
     */
    public function set_default($address_id) {
        $return = array('code' => 0, 'message' => ' set adress default success ! ', 'data' => array());
		$data = MuserAdress::setDefaultAddress($address_id);
		if (empty($data)) {
            $return['code'] = 1;
            $return['message'] = 'set adress default failed !';
			$return['data']['nums'] = 0;
            return $return;
        }
        $return['data']['nums'] = $data;
		return json($return);	
    }
	
	


}
